﻿using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CozeDto.ConsumptionHistoryDtos;
using CozeDomain.IServices;
using D.UtilFreesqlOrm;

namespace CozeWebApi
{
    /// <summary>
    /// 用户消费记录模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class ConsumptionHistoryController : BaseApiController
    {
        private readonly IConsumptionHistoryService _consumptionHistoryService;
        public ConsumptionHistoryController(IConsumptionHistoryService consumptionHistoryService)
        {
            _consumptionHistoryService = consumptionHistoryService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddConsumptionHistory"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddConsumptionHistory reqAddConsumptionHistory)
        {
            reqAddConsumptionHistory.CreateUser = Account;
            return await _consumptionHistoryService.AddConsumptionHistoryAsync(reqAddConsumptionHistory);
        }

        /// <summary>
        /// 删除，多个逗号隔开
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _consumptionHistoryService.DeleteConsumptionHistoryAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateConsumptionHistory"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateConsumptionHistory reqUpdateConsumptionHistory)
        {
            reqUpdateConsumptionHistory.Id = id;
            reqUpdateConsumptionHistory.UpdateUser = Account;
            return await _consumptionHistoryService.UpdateConsumptionHistoryAsync(reqUpdateConsumptionHistory);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepConsumptionHistoryInfo>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = LoginId;
            }
            return await _consumptionHistoryService.GetConsumptionHistoryAsync(id);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepConsumptionHistoryInfo>>> Pages([FromBody] ReqQueryConsumptionHistoryPage reqQueryConsumptionHistoryPage)
        {
            return await _consumptionHistoryService.QueryConsumptionHistoryPagesAsync(reqQueryConsumptionHistoryPage);
        }
    }
}
